<template>
  <div class="analysis-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="data-overview">
          <template #header>
            <div class="card-header">
              <span>农场数据分析</span>
              <el-date-picker
                v-model="dateRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="handleDateChange"
              />
            </div>
          </template>

          <el-row :gutter="20">
            <el-col :span="8">
              <el-card shadow="hover" class="data-card">
                <template #header>
                  <div class="data-card-header">
                    <span>土壤状况</span>
                  </div>
                </template>
                <div class="data-content">
                  <el-progress type="dashboard" :percentage="soilHealth" />
                  <div class="data-details">
                    <p>pH值: {{ soilData.pH }}</p>
                    <p>湿度: {{ soilData.moisture }}%</p>
                    <p>温度: {{ soilData.temperature }}°C</p>
                  </div>
                </div>
              </el-card>
            </el-col>

            <el-col :span="8">
              <el-card shadow="hover" class="data-card">
                <template #header>
                  <div class="data-card-header">
                    <span>作物生长</span>
                  </div>
                </template>
                <div class="data-content">
                  <el-progress type="dashboard" :percentage="cropHealth" />
                  <div class="data-details">
                    <p>生长阶段: {{ cropData.stage }}</p>
                    <p>健康度: {{ cropData.health }}%</p>
                    <p>预计产量: {{ cropData.expectedYield }}kg</p>
                  </div>
                </div>
              </el-card>
            </el-col>

            <el-col :span="8">
              <el-card shadow="hover" class="data-card">
                <template #header>
                  <div class="data-card-header">
                    <span>环境监测</span>
                  </div>
                </template>
                <div class="data-content">
                  <el-progress type="dashboard" :percentage="environmentHealth" />
                  <div class="data-details">
                    <p>温度: {{ environmentData.temperature }}°C</p>
                    <p>湿度: {{ environmentData.humidity }}%</p>
                    <p>光照: {{ environmentData.light }}lux</p>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>AI 分析报告</span>
              <el-button type="primary" @click="generateReport">生成报告</el-button>
            </div>
          </template>
          <div class="report-content" v-loading="reportLoading">
            <div v-if="report" v-html="report"></div>
            <el-empty v-else description="暂无分析报告" />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

// 日期范围
const dateRange = ref<[Date, Date] | null>(null)

// 模拟数据
const soilHealth = ref(85)
const cropHealth = ref(92)
const environmentHealth = ref(88)

const soilData = ref({
  pH: 6.5,
  moisture: 65,
  temperature: 25
})

const cropData = ref({
  stage: '生长期',
  health: 92,
  expectedYield: 2500
})

const environmentData = ref({
  temperature: 26,
  humidity: 70,
  light: 45000
})

// 报告相关
const report = ref('')
const reportLoading = ref(false)

const handleDateChange = (val: [Date, Date] | null) => {
  if (val) {
    // TODO: 根据日期范围获取数据
    console.log('日期范围变化:', val)
  }
}

const generateReport = async () => {
  reportLoading.value = true
  try {
    // TODO: 调用 DeepSeek API 生成报告
    setTimeout(() => {
      report.value = `
        <h3>农场数据分析报告</h3>
        <p>根据当前数据分析，您的农场整体状况良好，具体分析如下：</p>
        <ul>
          <li>土壤状况：pH值适中，湿度良好，适合当前作物生长</li>
          <li>作物生长：处于最佳生长期，健康度较高，预计产量可观</li>
          <li>环境条件：温度湿度适宜，光照充足</li>
        </ul>
        <p>建议：</p>
        <ul>
          <li>继续保持当前的水肥管理方案</li>
          <li>注意观察作物生长情况，及时防治病虫害</li>
          <li>根据天气预报，做好极端天气的防范工作</li>
        </ul>
      `
      reportLoading.value = false
    }, 2000)
  } catch (error) {
    ElMessage.error('生成报告失败')
    reportLoading.value = false
  }
}
</script>

<style scoped>
.analysis-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-card {
  height: 100%;
}

.data-card-header {
  font-weight: bold;
}

.data-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.data-details {
  width: 100%;
  text-align: center;
}

.data-details p {
  margin: 5px 0;
}

.mt-20 {
  margin-top: 20px;
}

.report-content {
  min-height: 200px;
  padding: 20px;
}
</style>
